layui.define(['jquery', 'layer', 'element', 'notice', 'axios','my_form'], function (exports) {
    var $ = layui.jquery
        , layer = layui.layer
        , notice = layui.notice
        , my_form =layui.my_form;
    //头部搜索按钮操作
    $(".header-center-search-type li a").click(function () {
        let info = $(this).data('info');
        let text = $(this).text();
        let type = $(this).data('type');
        $("#search-text").html(text + '<span class="layui-nav-more"></span>');
        $("#search-input").attr('placeholder', info);
        $("#search-btn").attr('data-type', type);
    });
    //底部固定栏操作
    var _footer_menu_index;
    $(".footer-bar-menu-box li").on('mouseenter', function () {
        let dom = $(this);
        let info = $(this).attr('data-title');
        _footer_menu_index = layer.tips(info, dom, {
            shade: 0
            , closeBtn: false
            , time: 0
            , anim: 5
            , tips: [4, '#000']
        })
    }).on('mouseleave', function () {
        layer.close(_footer_menu_index);
    })

    //返回顶部
    $(".goto-top").click(function () {
        $('html,body').animate({
            'scrollTop': '0'
        }, 500)
    });

    // 表单效果
    var layui_form_input = $(".form-box .layui-form-item").find(".layui-input");
    layui_form_input.on("focusin", function () {
        $(this).closest(".layui-form-item").addClass("layui-input-item-active");
    }).on("input", function () {
        $(this).closest(".layui-form-item").addClass("layui-input-item-active");
    }).on("focusout", function () {
        0 === $(this).val().length ?
            $(this).closest(".layui-form-item").removeClass("layui-input-item-active").removeClass(
                "layui-input-item-filled") :
            $(this).closest(".layui-form-item").removeClass("layui-input-item-active").addClass("layui-input-item-filled")
    });

    //搜索内容变更
    $(" .header-center-search #search-btn").on('click', function () {
        let keyword = $(this).siblings('input').val()
            , site_name = $("#search-text").text()
            , type = $(this).attr('data-type')
            , stringify = my_form.stringify({k: keyword, type: type})
            , localInfo = window.location;
        if (keyword === '') {
            notice.error('搜索内容不能为空');
            return;
        }
        switch (type) {
            case 'baidu':
                url = 'https://www.baidu.com/s?tn=baidu&si=' + localInfo.host + '&wd=' + keyword
                break;
            case 'bing':
                url = 'https://cn.bing.com/search?q=' + keyword
                break;
            case '360':
                url = 'https://www.so.com/s?q=' + keyword + '&ie=utf8&src=zz_52zsj.com&site=52zsj.com&rg=0';
                break;
            default:
                url = localInfo.protocol + '//' + localInfo.host
                break;
        }
        if (type === 'site') {
            location.href = url + '/search?' + stringify
            return false
        } else {
            layer.msg('确定跳转到 ' + site_name + '<br />搜索 ' + keyword, {
                time: 20000, //20s后自动关闭
                btn: ['确定', '取消'],
                yes: function (index) {
                    window.open(url)
                    layer.close(index)
                }
            });
        }
    });
    //评论按钮
    $(document).on('click', '.comment-reply-btn', function () {
        // 创建DIV
        var div = document.createElement('div');
        div.className = 'main-comment-list-box-reply clear';
        var form_html = $("#comment").html();
        div.innerHTML = form_html;
        $(div).find('.comment-push').html('回复').attr('lay-filter', 'form_reply');
        $(div).find('.comment-cancel').removeClass('layui-hide');
        var parent_node = $(this).parent();
        let reply_user_id = parent_node.attr('data-reply-user-id');
        let reply_user_name = parent_node.attr('data-reply-user-name');
        let pid = parent_node.attr('data-pid');

        $(div).find('input[name="reply_user_id"]').val(reply_user_id);
        $(div).find('input[name="reply_user_name"]').val(reply_user_name);
        $(div).find('input[name="pid"]').val(pid);

        if (parent_node.find('.main-comment-list-box-reply').length > 0) {
            return false;
        }
        parent_node.append(div);
        var placeholder = parent_node.data('reply-user-name');
        placeholder = (typeof placeholder === 'undefined' && placeholder !== '') ? '多少说点什么吧' : '回复:@' +
            placeholder;
        $(div).find('textarea').attr('placeholder', placeholder);
        $(this).parents().siblings().find('.main-comment-list-box-reply').remove()
    })

    $(document).on('click', '.comment-cancel', function () {
        // 直接干掉
        $('.main-comment-list-box-reply').remove();
    })
    $(document).on('click', '.show', function () {
        var that = $(this);
        if (that.parent().siblings('.main-comment-list-box-children').hasClass(
            'main-comment-list-box-children-more')) {
            that.text('——点击展开——')
        } else {
            that.text('——点击收起——')
        }
        that.parent().siblings('.main-comment-list-box-children').toggleClass(
            'main-comment-list-box-children-more');
    });


    $(document).on('click', '.footer-bar-menu-box .like', function () {
        let parent_ul = $(this).parents('ul');
        let parent_li = $(this).parent('li');
        let text = parent_li.attr('data-title');

        let type = parent_ul.attr('data-type');
        let link = parent_ul.attr('data-link');
        let id = parent_ul.attr('data-id');
        let options = {
            data: {type: type, id: id, action: 'like'},
            method: 'post',
            url: link,
        }
        my_form.request(options, function (ret) {
            var rt = /(.+)?(?:\(|（)(.+)(?=\)|）)/.exec(text);
            let a = rt[1] + '(' + ++rt[2] + ')';
            parent_li.attr('data-title', a);
        });
    })

    $(document).on('click', '.footer-bar-menu-box .dislike', function () {
        let parent_ul = $(this).parents('ul');
        let parent_li = $(this).parent('li');
        let text = parent_li.attr('data-title');

        let type = parent_ul.attr('data-type');
        let link = parent_ul.attr('data-link');
        let id = parent_ul.attr('data-id');
        let options = {
            data: {type: type, id: id, action: 'dislike'},
            method: 'post',
            url: link,
        }
        my_form.request(options, function (ret) {
            var rt = /(.+)?(?:\(|（)(.+)(?=\)|）)/.exec(text);
            let a = rt[1] + '(' + ++rt[2] + ')';
            parent_li.attr('data-title', a);
        });
    })

    exports("common",
        {}
    );
})
